<?php

require_once 'common/common.php';
require_once 'common/conn.php';

$usercode = getParameter("usercode");
$nickname = getParameter("nickname");
$email = getParameter("email");

$db = new DBC();
if ($nickname != "") {
    $sql = "SELECT devicecode AS usercode,nickname,sex,gold,headId,(SELECT url FROM t_hibaby_property WHERE code = headId) AS headUrl FROM t_device WHERE nickname = '" . $nickname . "'";
} else if ($email != "") {
    $sql = "SELECT devicecode AS usercode,nickname,sex,gold,headId,(SELECT url FROM t_hibaby_property WHERE code = headId) AS headUrl FROM t_device WHERE email = '" . $email . "'";
}

if (isset($sql)) {
    /*
      $data = $db->fetchData($sql);
      if ($data != false && count($data) > 0) {
      $usercode = $data[0]['usercode'];
      $sql = "SELECT usercode,SUM(amount) AS amount FROM t_gold_record WHERE usercode = '" . $usercode . "'";
      $gold = $db->fetchData($sql);
      if ($gold != false && count($gold) > 0) {
      $amount = $gold[0]['amount'];
      $sql = "UPDATE t_device SET gold = " . $amount . " WHERE devicecode = '" . $usercode . "'";
      $db->execute($sql);
      }
      } */
}

if ($nickname != "") {
    $sql = "SELECT devicecode AS usercode,nickname,sex,gold,headId,(SELECT url FROM t_hibaby_property WHERE code = headId) AS headUrl FROM t_device WHERE nickname = '" . $nickname . "'";
} else if ($email != "") {
    $sql = "SELECT devicecode AS usercode,nickname,sex,gold,headId,(SELECT url FROM t_hibaby_property WHERE code = headId) AS headUrl FROM t_device WHERE email = '" . $email . "'";
} else if ($usercode != "") {
    $sql = "SELECT devicecode AS usercode,nickname,sex,gold,headId,(SELECT url FROM t_hibaby_property WHERE code = headId) AS headUrl FROM t_device WHERE devicecode = '" . $usercode . "'";
}
$data_result = array();
if (isset($sql)) {
    $data_result = $db->fetchData($sql);
    if (checkArrayNotEmpty($data_result)) {
        for ($i = 0; $i < count($data_result); $i++) {
            if (isNull($data_result[$i]['nickname'])) {
                $data_result[$i]['nickname'] = "";
            }
        }
    }
}
echo json_encode($data_result);
?>
